package com.example.demo.thread.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import com.example.demo.entity.server.ServerListenEntity;
import com.example.demo.entity.server.ServerReportEntity;
import com.example.demo.server.dao.ServerListenDao;
import com.example.demo.util.DateUtil;
import com.example.demo.util.PrimaryKeyUtil;

import lombok.extern.slf4j.Slf4j;


/**
 * 
 * @Title: ListenServerThread.java
 * @Package com.jysp.dictionary_manager.thread
 * @Description: 监听服务的线程
 * @author 许子文
 * @date 2020年8月26日 下午2:35:40
 *
 */
@Slf4j
public class ListenServerThread extends Thread{
	
	private ServerListenEntity serverListenEntity;
	
	private ServerListenDao serverListenDao;

	public ListenServerThread(ServerListenEntity serverListenEntity, ServerListenDao serverListenDao) {
		super();
		this.serverListenEntity = serverListenEntity;
		this.serverListenDao = serverListenDao;
	}

	public ListenServerThread() {
		super();
	}
	
	@Override
	public void run() {
		String server_id = serverListenEntity.getKey_id();
		String ip = serverListenEntity.getServer_ip();
		Integer port = serverListenEntity.getServer_port();
		Integer out_time = serverListenEntity.getConnect_out_time();
		String server_name = serverListenEntity.getServer_name();
		String oldStatus = serverListenEntity.getServer_status();
		String server_status = "0";
		
		long resp_time = 100000l;
		
		Socket socket = new Socket();
		
		long startTime = System.currentTimeMillis();
		try {
			socket.connect(new InetSocketAddress(ip, port), out_time);
			long endTime = System.currentTimeMillis();
			resp_time = endTime - startTime;
			boolean isConnec = socket.isConnected();
			if(isConnec) {
				server_status = "1";
			}
			socket.close();
			log.debug("【{}】服务响应时间为{}ms",server_name,resp_time);
		} catch (IOException e) {
			log.error("【{}】服务响应超时",server_name);
		}
		
		ServerListenEntity updateParam = new ServerListenEntity();
		
		updateParam.setKey_id(serverListenEntity.getKey_id());
		updateParam.setServer_status(server_status);
		updateParam.setResp_time((int)resp_time);
		updateParam.setUpdate_time(DateUtil.getNowDateTime());
		
		try {
			serverListenDao.updateServerParam(updateParam);
		} catch (Exception e) {
			log.error("更新【{}】服务状态与响应时间失败",server_name,e);
			return;
		}
		
		//如果状态改变,记录状态变化到服务报告中去
		if(!server_status.equals(oldStatus)) {
			
			
			String report_time = DateUtil.getNowDateTime();
			
			String server_action = null;
			String report_content = null;
			
			if("2".equals(oldStatus)&&"1".equals(server_status)) {
				log.debug("初始化成功");
				server_action = "<font color=\"#2EA01C\">初始化</font>";
				
				report_content = "初始化监听服务成功!";
			} else if("2".equals(oldStatus)&&"0".equals(server_status)) {
				log.debug("初始化失败");
				server_action = "<font color=\"#D80000\">初始化</font>";
				
				report_content = "初始化监听服务失败";
			} else if("1".equals(oldStatus)&&"0".equals(server_status)) {
				log.debug("服务中断");
				server_action = "<font color=\"#D80000\">中断</font>";
				
				report_content = String.format("【%s】服务在%s左右异常停止", server_name,report_time);
			} else if("0".equals(oldStatus)&&"1".equals(server_status)) {
				log.debug("服务恢复");
				server_action = "<font color=\"#2EA01C\">恢复</font>";
				
				report_content = String.format("【%s】服务在%s左右恢复了运行", server_name,report_time);
			}
			
			ServerReportEntity report = new ServerReportEntity();
			report.setKey_id(PrimaryKeyUtil.getUUIDWithOutLink());
			report.setServer_id(server_id);
			report.setReport_time(report_time);
			report.setReport_content(report_content);
			report.setServer_action(server_action);
			
			try {
				serverListenDao.addServerReport(report);
			} catch (Exception e) {
				log.error("【{}】服务添加活动报告失败",server_name,e);
				return;
			}
			
		}
		
		
	}
}
